{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3e0e0358",
   "metadata": {},
   "source": [
    "# 习题1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f04c18c",
   "metadata": {},
   "source": [
    "**<font size=5>1.</font>**求下列排列得逆序数\n",
    "\n",
    "(1) 1 2 3 4 (2) 4 1 3 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f421cbe1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please input the number: 4 1 3 2\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "def inverse_number(string):\n",
    "    ans = 0\n",
    "    for i in range(len(string)):\n",
    "        for j in range(i):\n",
    "            if string[j] > string[i]:\n",
    "                ans += 1\n",
    "    return ans\n",
    "\n",
    "print(inverse_number(input(\"Please input the number: \")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76152b1a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3636b5c0",
   "metadata": {},
   "source": [
    "**<font size=5>3.</font>**解方程\n",
    "\n",
    "$\\left|\\begin{matrix} x & 3 & 4\\\\ -1 & x & 0\\\\ 0 & x & 1\\end{matrix}\\right|=0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4855c62c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "\n",
    "arr = np.array([['x',3,4],[-1,'x',0],[0,'x',1]])\n",
    "M = sp.Matrix(arr)\n",
    "x = sp.Symbol('x')\n",
    "\n",
    "sp.solve(M.det(),x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "def3d0df",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "81a8c732",
   "metadata": {},
   "source": [
    "**<font size=5>4.</font>**根据行列式得定义计算下列得行列式：\n",
    "\n",
    "(1)$\\left|\\begin{matrix} a & 0 & 0 & b\\\\ 0 & c & d & 0\\\\ 0 & e & f & 0\\\\ g & 0 & 0 & h\\end{matrix}\\right|$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2e419d9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left(a h - b g\\right) \\left(c f - d e\\right)$"
      ],
      "text/plain": [
       "(a*h - b*g)*(c*f - d*e)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['a',0,0,'b'],[0,'c','d',0],[0,'e','f',0],['g',0,0,'h']])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.symbols('a b c d e f g h')\n",
    "M.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c08eaea5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "1980e260",
   "metadata": {},
   "source": [
    "**<font size=5>5.</font>**行列式$\\left|\\begin{matrix} 5x & x & 1 & x\\\\ 1 & x & 1 & -x\\\\ 3 & 2 & x & 1\\\\ 3 & 1 & 1 & x\\end{matrix}\\right|$是x的几次多项式？分别求出$x^4$和$x^3$项的系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2bc8ca9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 5 x^{4} - 2 x^{3} - 18 x^{2} + 12 x - 1$"
      ],
      "text/plain": [
       "5*x**4 - 2*x**3 - 18*x**2 + 12*x - 1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['5*x','x',1,'x'],[1,'x',1,'-x'],[3,2,'x',1],[3,1,1,'x']])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.Symbol('x')\n",
    "M.det()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d53905d",
   "metadata": {},
   "source": [
    "因此，行列式是4次多项式，$x^4$和$x^3$的系数分别为$5$和$-2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8b57451",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "56460c3e",
   "metadata": {},
   "source": [
    "**<font size=5>6.</font>**用行列式性质证明$\\left|\\begin{matrix} a_1+kb_1 & b_1+c_1 & c_1\\\\ a_2+kb_2 & b_2+c_2 & c_2\\\\ a_3+kb_3 & b_3+c_3 & c_3\\end{matrix}\\right|=\\left|\\begin{matrix} a_1 & b_1 & c_1\\\\ a_2 & b_2 & c_2\\\\ a_3 & b_3 & c_3\\end{matrix}\\right|$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0037d84b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a_{1} b_{2} c_{3} - a_{1} b_{3} c_{2} - a_{2} b_{1} c_{3} + a_{2} b_{3} c_{1} + a_{3} b_{1} c_{2} - a_{3} b_{2} c_{1}$"
      ],
      "text/plain": [
       "a1*b2*c3 - a1*b3*c2 - a2*b1*c3 + a2*b3*c1 + a3*b1*c2 - a3*b2*c1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['a1+k*b1','b1+c1','c1'],['a2+k*b2','b2+c2','c2'],['a3+k*b3','b3+c3','c3']])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.Symbol('a_1 b_1 c_1 a_2 b_2 c_2 a_3 b_3 c_3 k')\n",
    "M.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8c12ede2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a_{1} b_{2} c_{3} - a_{1} b_{3} c_{2} - a_{2} b_{1} c_{3} + a_{2} b_{3} c_{1} + a_{3} b_{1} c_{2} - a_{3} b_{2} c_{1}$"
      ],
      "text/plain": [
       "a1*b2*c3 - a1*b3*c2 - a2*b1*c3 + a2*b3*c1 + a3*b1*c2 - a3*b2*c1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['a1','b1','c1'],['a2','b2','c2'],['a3','b3','c3']])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.Symbol('a1 b1 c1 a2 b2 c2 a3 b3 c3 k')\n",
    "M.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58493bf3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a31f6d12",
   "metadata": {},
   "source": [
    "**<font size=5>7.</font>**计算行列式$D=\\left|\\begin{matrix} 3 & 4 & 6\\\\ -2 & 2 & -4 \\\\ 4 & -7 & 8\\end{matrix}\\right|$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b9cdd408",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['3','4','6'],['-2','2','-4'],['4','-7','8']])\n",
    "M=sp.Matrix(arr)\n",
    "M.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce91b234",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "16523ae8",
   "metadata": {},
   "source": [
    "**<font size=5>10.</font>**计算4阶行列式$D=\\left|\\begin{matrix} 1 & 1 & 1 & 1\\\\ 1 & 1 & -4 & 1\\\\ 1 & -4 & 1 & 1\\\\ -4 & 1 & 1 & 1\\end{matrix}\\right|$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4aba466a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle -50$"
      ],
      "text/plain": [
       "-50"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "D=np.array([[1, 1, 1,1],\n",
    "            [1, 1, -1,1],\n",
    "            [1, -4, 1,1],\n",
    "            [-4,1,1,1]])\n",
    "sp.Matrix(D).det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70c66e24",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f83fca25",
   "metadata": {},
   "source": [
    "**<font size=5>11.</font>**计算行列式$D=\\left|\\begin{matrix} 4 & 1 & 1 & 1\\\\ 1 & 4 & 1 & 1\\\\ 1 & 1 & 4 & 1\\\\ 1 & 1 & 1 & 4\\end{matrix}\\right|$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "204865aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 189$"
      ],
      "text/plain": [
       "189"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['4','1','1','1'],['1','4','1','1'],['1','1','4','1'],['1','1','1','4']])\n",
    "M=sp.Matrix(arr)\n",
    "M.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb602830",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b1d96b2f",
   "metadata": {},
   "source": [
    "**<font size=5>12.</font>**计算行列式$D=\\left|\\begin{matrix} x & a_1 & a_2 & a_3\\\\ b_1 & 1 & 0 & 0\\\\ b_2 & 0 & 2 & 1\\\\ b_n & 0 & 0 & 3\\end{matrix}\\right|$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bb5b239f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - 6 a_{1} b_{1} - 3 a_{2} b_{2} + a_{2} bn - 2 a_{3} bn + 6 x$"
      ],
      "text/plain": [
       "-6*a1*b1 - 3*a2*b2 + a2*bn - 2*a3*bn + 6*x"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['x','a1','a2','a3'],['b1','1','0','0'],['b2','0','2','1'],['bn','0','0','3']])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.symbols('x a_1 a_1 a_2 b_1 b_2 b_n')\n",
    "M.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ec5e4ef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fadb4800",
   "metadata": {},
   "source": [
    "**<font size=5>13.</font>**计算行列式$D=\\left|\\begin{matrix} a & b & c & d\\\\ a & a+b & a+b+c & a+b+c+d\\\\ a & 2a+b & 3a+2b+c & 4a+3b+2c+d\\\\ a & 3a+b & 6a+3b+c & 10a+6b+3c+d\\end{matrix}\\right|$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2b481284",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle a^{4}$"
      ],
      "text/plain": [
       "a**4"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['a','b','c','d'],['a','a+b','a+b+c','a+b+c+d'],['a','2*a+b','3*a+2*b+c','4*a+3*b+2*c+d'],['a','3*a+b','6*a+3*b+c','10*a+6*b+3*c+d']])\n",
    "M=sp.Matrix(arr)\n",
    "M.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1937e64a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b325268d",
   "metadata": {},
   "source": [
    "**<font size=5>14.</font>**解方程$\\left|\\begin{matrix} 1 & 2 & 1 & 1\\\\ 1 & x & 2 & 3\\\\ 0 & 0 & x & 2\\\\ 0 & 0 & 2 & x\\end{matrix}\\right|=0$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e60b5d90",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2, 2]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([[1,2,1,1],[1,'x',2,3],[0,0,'x',2],[0,0,2,'x']])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.Symbol('x')\n",
    "print(sp.solve(M.det(),x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e726b041",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "1ce34bdb",
   "metadata": {},
   "source": [
    "**<font size=5>15.</font>**$\\lambda$取何值时，线性方程组$\\begin{equation*}\n",
    "     \\begin{cases}\n",
    "         (\\lambda-3)x_1-x_2+x_4=0 \\\\\n",
    "         -x_1+(\\lambda-3)x_2+x_3=0 \\\\ \n",
    "         x_2+(\\lambda-3)x_3-x_4=0\\\\\n",
    "         x_1-x_3+(\\lambda-3)x_4=0\n",
    "     \\end{cases}\n",
    " \\end{equation*}$有非零解?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1d7bf920",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "arr = np.array([['x-3',-1,0,1],[-1,'x-3',1,0],[1,0,'x-3',-1],[1,0,-1,'x-3']])\n",
    "M=sp.Matrix(arr)\n",
    "x = sp.Symbol('x')\n",
    "print(sp.solve(M.det(),x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7fd2928",
   "metadata": {},
   "source": [
    "所以，$\\lambda=2或3或5$时，方程有非零解."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0cc202e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
